﻿using System;
using System.Linq;
using Buddy.BehaviorTree;
using Buddy.Common;
using Buddy.CommonBot;
using Buddy.Swtor;

using WingIt.Dynamics;

using Action = Buddy.BehaviorTree.Action;

namespace WingIt.Routines
{
    public static class AssassinMadness
    {
        [Class(CharacterClass.Inquisitor, AdvancedClass.Assassin, SkillTreeId.AssassinMadness)]
        [Behavior(BehaviorType.Combat)]
        public static Composite AssassinMadnessCombat()
        {
            return new PrioritySelector(
            Movement.StopInRange(0.3f),
            Spell.WaitForCast(),

            Spell.Cast("Lightning Charge", ret => !BuddyTor.Me.HasBuff("Lightning Charge")),

            Spell.Cast("Mark of Power", ret => !BuddyTor.Me.HasBuff("Mark of Power")),

            Spell.Cast("Unbreakable Will", ret => BuddyTor.Me.IsStunned),

            // Crowd controls!
                //Spell.Cast("Electrocute", onUnit => Helpers.Targets.FirstOrDefault(t => t.Toughness == CombatToughness.Strong), ret => Helpers.Targets.Count() > 3 && Helpers.Targets.FirstOrDefault(t => t.Toughness == CombatToughness.Strong) != null),
            Spell.Cast("Whirlwind", onUnit =>
            {
                var previousTarget = BuddyTor.Me.CurrentTarget;
                return
                Helpers.Targets.FirstOrDefault(
                t =>
                t != previousTarget && (t.Toughness == CombatToughness.Strong) && !t.IsDead) ??
                Helpers.Targets.FirstOrDefault(t => t != previousTarget);
            }, castWhen => Helpers.Targets.Count() >= 3),

            // Interrupts
            Spell.Cast("Jolt", ret => BuddyTor.Me.CurrentTarget.IsCasting),
            Spell.Cast("Overload", ret => BuddyTor.Me.CurrentTarget.IsCasting),
            Spell.Cast("Electrocute", ret => BuddyTor.Me.CurrentTarget.IsCasting),

            // Defensive Cooldowns
            Spell.Cast("Deflection", ret => BuddyTor.Me.InCombat && BuddyTor.Me.HealthPercent <= 80),
            Spell.Cast("Force Shroud", ret => BuddyTor.Me.InCombat && BuddyTor.Me.HealthPercent <= 70),

            // Buffs
            Spell.Cast("Recklessness", castWhen => BuddyTor.Me.InCombat && !BuddyTor.Me.HasBuff("Recklessness")),
            Spell.Cast("Force Cloak", castWhen => BuddyTor.Me.InCombat && !BuddyTor.Me.HasBuff("Dark Embrace")),
            Spell.Cast("Overcharge Saber", castWhen => BuddyTor.Me.InCombat && !BuddyTor.Me.HasBuff("Overcharge")),

            Spell.CastOnGround("Death Field", ret => Helpers.Targets.Count() >= 1, ret => BuddyTor.Me.CurrentTarget.Position),
            Spell.Debuff("Crushing Darkness", castWhen => BuddyTor.Me.HasBuff("Raze") && !BuddyTor.Me.CurrentTarget.HasDebuff("Crushed (Force)")),
            Spell.Debuff("Discharge", castWhen => !BuddyTor.Me.CurrentTarget.HasDebuff("Shocked (Force)")),
            Spell.Cast("Assassinate", castWhen => BuddyTor.Me.CurrentTarget.HealthPercent <= 30),
            Spell.Debuff("Creeping Terror", castWhen => !BuddyTor.Me.CurrentTarget.HasDebuff("Creeping Terror (Force)")),
            Spell.Cast("Maul", castWhen => BuddyTor.Me.HasBuff("Exploit Weakness")),
            Spell.Cast("Thrash", castWhen => BuddyTor.Me.ResourceStat > 45),
            Spell.Cast("Saber Strike"),
            Movement.MoveTo(ret => BuddyTor.Me.CurrentTarget.Position, 0.3f)

            );
        }

        [Behavior(BehaviorType.OutOfCombat)]
        public static Composite AssassinMadnessRest()
        {
            return new PrioritySelector(
            Spell.Cast("Seethe", ret => BuddyTor.Me.HealthPercent <= 90)
            );
        }

        //[Behavior(BehaviorType.Pull)]
        //public static Composite AssassinMadnessPull()
        //{
        // return new PrioritySelector(
        // Spell.CastOnGround("Death Field", ret => Helpers.Targets.Count() >= 1, ret => BuddyTor.Me.CurrentTarget.Position))
        //}
    }
}